package client.view;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import server.model.administration.MeetingService;
import server.model.meeting.Announcement;
import server.model.meeting.Meeting;
import server.model.meeting.MeetingInfo;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.meeting.Group;
import server.model.remotes.IMeetingService;


public class MenuGui extends JPanel {
	
	private GuiManager manager;

	private JLabel lblInfo;
	private JLabel lblMeetings;
	private JLabel lblAnnouncements;
	private JLabel lblGroups;
	private JLabel lblMeetingText;
	
	private List lstMeetings;
	private List lstGroups;
	private List lstAnnouncements;
	
	private JButton btnUserInfo;
	private JButton btnCreateMeeting;
	private JButton btnAttendMeeting;
	private JButton btnCreateGroup;
	private JButton btnCreateAnnouncement;
	
	private Iterator<Participant> participant;

	public MenuGui(GuiManager manager) {
		this.manager = manager;
		
		setSize(585, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
	}

	private void initGUI() {
		lblInfo = new JLabel("Hoofd Menu");
		lblInfo.setBounds(31, 12, 300, 20);
		lblInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));
		
		lblMeetings = new JLabel("Mijn geplande vergaderingen");
		lblMeetings.setBounds(31, 44, 200, 16);

		lblAnnouncements = new JLabel("Mededelingen");
		lblAnnouncements.setBounds(344, 44, 118, 16);
		
		lblGroups = new JLabel("Mijn groepen");
		lblGroups.setBounds(344, 164, 97, 16);
		
		lstGroups = new List();
		lstGroups.setBounds(344, 186, 196, 92);
		
		lstAnnouncements = new List();	
		lstAnnouncements.setBounds(344, 65, 196, 87);
	
		lstMeetings = new List();
		lstMeetings.setBounds(31, 72, 250, 206);	
		
		lblMeetingText = new JLabel("Vergadering:");
		lblMeetingText.setBounds(32, 290, 150, 22);
		
		btnCreateMeeting = new JButton("Toevoegen");
		btnCreateMeeting.setBounds(31, 320, 98, 22);
		btnCreateMeeting.addActionListener(new btnCreateMeetingListener());
		
		
		btnAttendMeeting = new JButton("Bijwonen");
		btnAttendMeeting.setBounds(161, 320, 120, 22);
		btnAttendMeeting.addActionListener(new btnAttendMeetingListener());
		
		btnCreateGroup = new JButton("Nieuwe groep");
		btnCreateGroup.setBounds(420, 290, 120, 22);
		btnCreateGroup.addActionListener(new btnCreateGroupListener());
		
		btnCreateAnnouncement = new JButton("Toevoegen");
		btnCreateAnnouncement.setBounds(430, 44, 110, 16);
		btnCreateAnnouncement.addActionListener(new btnCreateAnnoucement());
		
		btnUserInfo = new JButton("Gegevens wijzigen");
		btnUserInfo.setBounds(150, 12, 175, 23);
		btnUserInfo.addActionListener(new btnUserInfo());
		
		add(lblInfo);
		add(lblMeetings);
		add(lblAnnouncements);
		add(lblGroups);
		add(lblMeetingText);
				
		add(lstMeetings);
		add(lstGroups);
		add(lstAnnouncements);
	
		add(btnUserInfo);
		add(btnCreateMeeting);
		add(btnAttendMeeting);
		add(btnCreateGroup);	
		add(btnCreateAnnouncement);


		repaint();
	}
	public void reset()
	{
		DateFormat df = DateFormat.getDateInstance();
		DateFormat tf = DateFormat.getTimeInstance();
		IMeetingService mService = manager.getMeetingService();
		
		lstMeetings.removeAll();
		
			try {
				MeetingInfo mInfo[];
				mInfo = mService.getMeetingsInfo();
			
						
			for (int i = 0; i<mInfo.length; i++){
				String meetingStart = tf.format(mInfo[i].getStartDate());
				String meetingEnd = tf.format(mInfo[i].getEndDate());
								
				lstMeetings.add(mInfo[i].getName());			
			}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
			try {
				ArrayList<Announcement> aInfo = mService.getAnnouncements();
			
				for (Announcement announcement:aInfo){
					lstAnnouncements.add(announcement.getMessage());	
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}
	
	private class btnUserInfo implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {

			manager.switchScreen("UserInfo");
		}		
	}
	
	private class btnAttendMeetingListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.attendMeeting(lstMeetings.getSelectedItem());
		}		
	}
	
	private class btnCreateGroupListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("createGroupGui");
		}		
	}
	
	private class btnCreateMeetingListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("createMeetingGui");
		}		
	}	
	
	private class btnCreateAnnoucement implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("createAnnouncementGui");
		}		
	}	
}
